package com.xinsoft.controller.device;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xinsoft.common.Response;
import com.xinsoft.constant.Const;
import com.xinsoft.entity.dto.CusPatrolStandardParam;
import com.xinsoft.entity.po.CusDevicePatrolStandardRef;
import com.xinsoft.entity.po.CusDocument;
import com.xinsoft.entity.po.CusPatrolStandard;
import com.xinsoft.entity.po.SysFileHis;
import com.xinsoft.service.CusDevicePatrolStandardRefService;
import com.xinsoft.service.CusDocumentService;
import com.xinsoft.service.CusPatrolStandardService;
import com.xinsoft.service.SysFileHisService;
import com.xinsoft.validator.ValidatorUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName CusPatrolStandardController
 * @Description 巡检标准
 * @Author lijie
 * @Date 2023-6-13 12:53:16
 * @Version 1.0
 */
@RestController
@RequestMapping("/cus/patrol/standard")
@Api(value = "CusPatrolStandardController", tags = {"巡检标准控制层"})
public class CusPatrolStandardController {
	@Autowired
	private CusPatrolStandardService standardService;

	@Autowired
	private CusDevicePatrolStandardRefService refService;

	@Autowired
	private CusDocumentService cusDocumentService;

	@Autowired
	private SysFileHisService sysFileHisService;
	/**
	 * @Author Li9527
	 * @Description 巡检标准-新增
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("新增")
	@PostMapping("/add")
	public Response<?> add(@RequestBody CusPatrolStandard cusPatrolStandard) {
		// 参数校验
		ValidatorUtils.validateEntity(cusPatrolStandard);
		CusPatrolStandard oldObj = standardService.getOne(new QueryWrapper<CusPatrolStandard>().eq("standard_name",cusPatrolStandard.getStandardName()));
		if(oldObj != null){
			return Response.fail("该项巡检标准控已存在,请不要重复添加");
		}
		standardService.save(cusPatrolStandard);
		//保存图片
		if(CollectionUtil.isNotEmpty(cusPatrolStandard.getCusDocumentListDocument())){
			for (CusDocument cusDocument : cusPatrolStandard.getCusDocumentListDocument()) {
				cusDocument.setDeviceRefId(cusPatrolStandard.getId());
				cusDocument.setDeviceRefType(Const.DeviceRefType.PATROL_BASIC);
			}
			cusDocumentService.saveBatch(cusPatrolStandard.getCusDocumentListDocument());
		}
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 更新
	 * @Date 2023/5/17 9:57
	 * @params   dept
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("更新")
	@PostMapping("/edit")
	public Response<?> edit(@RequestBody CusPatrolStandard cusPatrolStandard) {
		// 参数校验
		ValidatorUtils.validateEntity(cusPatrolStandard);
		CusPatrolStandard oldObj = standardService.getOne(new QueryWrapper<CusPatrolStandard>().eq("standard_name",cusPatrolStandard.getStandardName()));
		if(oldObj != null && !oldObj.getId().equals(cusPatrolStandard.getId())){
			return Response.fail("该项巡检标准控已存在,请不要重复添加");
		}
		standardService.updateById(cusPatrolStandard);
		//删除图片关系
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("device_ref_id",cusPatrolStandard.getId());
		queryWrapper.eq("device_ref_type",Const.DeviceRefType.PATROL_BASIC);
		cusDocumentService.remove(queryWrapper);
		//保存图片
		if(CollectionUtil.isNotEmpty(cusPatrolStandard.getCusDocumentListDocument())){
			for (CusDocument cusDocument : cusPatrolStandard.getCusDocumentListDocument()) {
				cusDocument.setDeviceRefId(cusPatrolStandard.getId());
				cusDocument.setDeviceRefType(Const.DeviceRefType.PATROL_BASIC);
			}
			cusDocumentService.saveBatch(cusPatrolStandard.getCusDocumentListDocument());
		}
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 获取详情
	 * @Date 2023/5/17 9:57
	 * @params   id
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("获取详情")
	@GetMapping("/detail")
	public Response<?> detail(@RequestParam(value = "id", defaultValue = "", required = true) Integer id) {
		CusPatrolStandard cusPatrolStandard = standardService.getById(id);
		//照片列表
		QueryWrapper queryWrapper = new QueryWrapper<>();
		queryWrapper.select("document_id");
		queryWrapper.eq("device_ref_id",id);
		queryWrapper.eq("device_ref_type",Const.DeviceRefType.PATROL_BASIC);
		List<CusDocument> documentList = cusDocumentService.list(queryWrapper);
		cusPatrolStandard.setCusDocumentListDocument(documentList);
		// 图片列表
		if(CollectionUtil.isNotEmpty(documentList)){
			List<Integer> fileIds = documentList.stream()
					.map(CusDocument::getDocumentId)
					.collect(Collectors.toList());
			QueryWrapper queryFileWrapper = new QueryWrapper<>();
			queryFileWrapper.in("id",fileIds);
			List<SysFileHis> fileHisList = sysFileHisService.list(queryFileWrapper);
			cusPatrolStandard.setSysFileHisList(fileHisList);
		}
		return Response.succeed(cusPatrolStandard);
	}

	/**
	 * @Author Li9527
	 * @Description 删除
	 * @Date 2023/5/12 9:20
	 * @params   ids
	 * @return com.xinsoft.common.Response<?>
	 **/
	@ApiOperation("删除")
	@PostMapping("/del")
	public Response<?> del(@RequestBody List<Integer> ids) {
		standardService.removeByIds(ids);
		for (int i = 0; i < ids.size(); i++) {
			refService.remove(new QueryWrapper<CusDevicePatrolStandardRef>().eq("patrol_standard_id",ids.get(i)));
		}
		return Response.succeed();
	}

	/**
	 * @Author Li9527
	 * @Description 巡检标准控分页
	 * @Date 2023/5/18 10:51
	 * @params   param
	 * @return com.xinsoft.common.Response<?>
	 **/
	@PostMapping("/pageList")
	public Response<?> pageList(@Valid @RequestBody CusPatrolStandardParam param) {
		return Response.succeed(standardService.findPageListByParam(param));
	}
}
